import {useStore} from "@/pinia/user";

const baseUrl = "http://127.0.0.1:18084"
const tokenName = "liuJun-token"

function requestA(med, url, data) {

    return new Promise((resolve, reject) => {

        uni.request({
            url: baseUrl + url,
            method: med,
            data: data,
            header: {
                "liuJun-token": useStore().token
            },
            timeout: 3000,
            dataType: "JSON",
            success: (res) => {
                resolve(res)
            },
            fail: (err) => {
                reject(err)
            }

        })
    })
}

const get = (url, param) => {

    return new Promise((resolve, reject) => {

        uni.request({
            url: baseUrl + url + uni.$u.queryParams(param),
            method: "GET",
            header: {
                "liuJun-token": useStore().token
            },
            timeout: 3000,
            success: (res) => {
                resolve(res.data)
            },
            fail: (err) => {
                reject(err)
            }

        })
    })
}
const post = (url, data) => {
    return new Promise((resolve, reject) => {
        uni.request({
            url: baseUrl + url,
            method: "POST",
            header: {
                "liuJun-token": useStore().token
            },
            data: data,
            timeout: 3000,
            dataType: "JSON",
            success: (res) => {
                resolve(JSON.parse(res.data))
            },
            fail: (err) => {
                reject(err)
            }

        })
    })
}
const put = (url, data) => {
    return new Promise((resolve, reject) => {
        uni.request({
            url: baseUrl + url,
            method: "PUT",
            header: {
                "liuJun-token": useStore().token
            },
            data: data,
            timeout: 3000,
            dataType: "JSON",
            success: (res) => {
                resolve(JSON.parse(res.data))
            },
            fail: (err) => {
                reject(err)
            }

        })
    })
}
const deleteA = (url, data) => {
    return new Promise((resolve, reject) => {
        uni.request({
            url: baseUrl + url,
            method: "DELETE",
            header: {
                "liuJun-token": useStore().token
            },
            data: data,
            timeout: 3000,
            dataType: "JSON",
            success: (res) => {
                resolve(JSON.parse(res.data))
            },
            fail: (err) => {
                reject(err)
            }

        })
    })
}
const uploadFile = (url, file, formData) => {

    return new Promise((resolve, reject) => {

        uni.uploadFile({
            url: baseUrl + url,
            filePath: file,
            name: 'file',
            header: {
                "liuJun-token": useStore().token
            },
            formData: formData || null,
            success: (result) => {
                resolve(JSON.parse(result.data))
            },
            fail: (err) => {
                reject(err)
            }


        })

    })

}

const request = {
    A: (med, url, data) => requestA(med, url, data),
    get: (url, data) => get(url, data),
    post: (url, data) => post(url, data),
    put: (url, data) => put(url, data),
    delete: (url, data) => deleteA(url, data),
    uploadFile: (url, file, formData) => uploadFile(url, file, formData)
}

uni.addInterceptor('request', {
    invoke(args) {
        uni.showLoading({
            title: '加载中',
        })
    },
    success(args) {


    },
    fail(err) {

    },
    complete(res) {
        uni.hideLoading()
    }
})

export default request
